// ignore_for_file: prefer_interpolation_to_compose_strings, unnecessary_string_interpolations
import 'dart:math';

import 'package:flutter_kit_plus/flutter_kit.dart';

import '../../model/model.dart';
import '../../model/model_default.dart';
import '../log.dart';
import '../main.dart';
import '../notify/globalState.dart';

const String Service_UUID = 'FAA0';
const String WriteChar_UUID = 'FAA1';
const String ReadChar_UUID = 'FAA1';
const ble_header = 0xAA55;
const ble_eq = 0xAA;
const ble_eq_unit = 0xAB;
const ble_eq_reset = 0xFF02;
const ble_phase = 0xBB;
const ble_delay = 0xCC;
const ble_mute = 0xDB;
const ble_mute_all = 0xEE;
const ble_high = 0xDD;
const ble_low = 0xDE;
const ble_pass = 0xFF;
const ble_sum = 0xAA83F0;
const ble_noise = 0xCCCC;
const ble_reset_dsp = 0xAAFFFF;
const _deviceData = [0x47, 0x45, 0x54, 0x44, 0x53, 0x50, 0x54, 0x59, 0x55, 0x55];

class SendData {
  static void init() {
    globalState.addListener(_onStateChange);
  }

  static List<int> getBytes(NotifyData newdata) {
    List<int> bytes = [];
    switch (newdata.key) {
      ///混音
      case '2-CH':
      case '4-CH':
      case 'SUM':
        ChannelModel ch = newdata.body;
        int num = 192 + ch.num;
        List<int> list = [];
        for (var $0 in ch.input_list) {
          list.add($0 == 0 ? 0 : 1);
        }
        bytes += ble_header.bytes;
        bytes += ble_sum.bytes;
        bytes += num.bytes;
        bytes += list;
        bytes = bytes.padRight(14);

      ///通道的静音，音量设置
      case 'MUTE':
      case 'DB':
        ChannelModel ch = newdata.body;
        int num = 192 + ch.num;
        bytes += ble_header.bytes;
        bytes += ble_mute.bytes;
        bytes += num.bytes;
        bytes += (ch.mute ? 1 : 0).bytes;
        bytes += (ch.db.toInt() * 10).abs().bytes.padLeft(2);
        bytes = bytes.padRight(14);

      ///正反相
      case 'PHASE':
        ChannelModel ch = newdata.body;
        int num = 192 + ch.num;
        bytes += ble_header.bytes;
        bytes += ble_phase.bytes;
        bytes += num.bytes;
        bytes += (ch.phase ? 0 : 1).bytes;
        bytes = bytes.padRight(14);

      ///通道eq设置
      case 'EQ':
        SignModel sign = newdata.body;
        List<String> list = newdata.message.split('&');
        int ch_num = list.first.float.toInt();
        int num = 192 + ch_num;
        int cmd = list.length > 1 ? ble_eq_unit : ble_eq;
        bytes += ble_header.bytes;
        bytes += cmd.bytes;
        bytes += num.bytes;
        bytes += sign.index.bytes;
        bytes += sign.freq.bytes.padLeft(2);
        bytes += (sign.Q * 100).toInt().bytes.padLeft(2);
        bytes += ((sign.gain + 20) * 10).toInt().bytes.padLeft(2);
        bytes = bytes.padRight(14);

      ///恢复通道默认eq
      case 'EQ_RESET':
        bytes += ble_header.bytes;
        bytes += ble_eq_reset.bytes;
        bytes += [0xC0];
        bytes = bytes.padRight(14);

      ///高低通滤波设置
      case 'FILTER':
        Passmodel pass = newdata.body;
        int ch_num = newdata.message.float.toInt();
        int num = 192 + ch_num;
        bytes += ble_header.bytes;
        bytes += pass.ishigh ? ble_high.bytes : ble_low.bytes;
        bytes += num.bytes;
        bytes += pass.type!.type.bytes;
        bytes += pass.type!.slope_hex.bytes;
        bytes += min(19999, pass.freq).bytes.padLeft(2);
        bytes = bytes.padRight(14);

      ///高低通直通
      case 'BYPASS':
      case 'RESETPASS':
        int ch_num = newdata.message.float.toInt();
        int num = 192 + ch_num;
        bytes += ble_header.bytes;
        bytes += ble_pass.bytes;
        bytes += num.bytes;
        bytes += newdata.key == 'BYPASS' ? [0x00] : [0x01];
        bytes = bytes.padRight(14);

      ///通道延迟
      case 'DELAY':
        ChannelModel ch = newdata.body;
        int num = 192 + ch.num;
        bytes += ble_header.bytes;
        bytes += ble_delay.bytes;
        bytes += num.bytes;
        if (ch.delay_type == 'ms') {
          bytes += (ch.delay.float * 100).toInt().bytes.padLeft(2);
        } else if (ch.delay_type == 'cm') {
          bytes += (ch.delay.float / 34.3 * 100).toInt().bytes.padLeft(2);
        } else if (ch.delay_type == 'in') {
          bytes += (ch.delay.float / 34.3 / 0.3937 * 100).toInt().bytes.padLeft(2);
        }
        bytes = bytes.padRight(14);

      ///总音量
      case 'MUTE_ALL':
        bytes += ble_header.bytes;
        bytes += ble_mute_all.bytes;
        bytes += (globalModel.mute ? 1 : 0).bytes;
        bytes += (globalModel.db.toInt() * 10).abs().bytes.padLeft(2);
        bytes = bytes.padRight(14);

      ///获取 设备数据
      case 'DEVICEDATA':
        bytes += ble_header.bytes;
        bytes += _deviceData;
        bytes = bytes.padRight(14);

      ///降噪
      case 'NOISE':
        int level = newdata.body;
        bytes += ble_header.bytes;
        bytes += ble_noise.bytes;
        bytes += level.bytes;
        bytes = bytes.padRight(14);
      case 'RESET_DSP':
        bytes += ble_header.bytes;
        bytes += ble_reset_dsp.bytes;
        bytes = bytes.padRight(14);
      case 'UPDATE':
        String text = newdata.body;
        bytes += text.bytes;
      default:
        break;
    }
    return bytes;
  }

  static void _onStateChange(NotifyData newdata) {
    List<int> bytes = getBytes(newdata);
    print('发送数据 = ${bytes.toHexstr(' |')}');
    if (bytes.isEmpty) {
      return;
    }
    if (isDebugLog) {
      ///日志
      var text = '发送：${bytes.toHexstr('|')}';
      LogNotifier.addLog(navigatorKey.currentContext, text);
    }

    BluetoothManager.instance.write(service_uuid: Service_UUID, characteristics_uuid: WriteChar_UUID, data: bytes);
  }
}
